package com.atl.controller.web;


import com.atl.common.BaseResponse;
import com.atl.common.DeleteRequest;
import com.atl.common.ErrorCode;
import com.atl.enity.po.AgentUser;
import com.atl.enity.web.dto.AgentUserAddDTO;
import com.atl.enity.web.dto.AgentUserSearchDTO;
import com.atl.service.IAgentUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 代理商用户表 前端控制器
 * </p>
 *
 * @author 
 * @since 2024-06-24
 */
@RestController
@RequestMapping("/web/agent-user")
@Api(tags = "代理用户-即商家，登录，注册，增删改查")
@Slf4j
public class AgentUserController {


    @Resource
    private IAgentUserService agentUserService;




    ///**
    // *
    // * 用户注册
    // *
    // * @param userRegisterRequestDTO 接受dto
    // * @return 返回用户id
    // */
    ////@ApiOperation("用户注册")
    //@PostMapping("/register")
    //public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequestDTO userRegisterRequestDTO) {
    //
    //    if (userRegisterRequestDTO == null) {
    //        return BaseResponse.error("传入用户数据为空");
    //    }
    //
    //    String userAccount = userRegisterRequestDTO.getUserAccount();
    //    String userPassword = userRegisterRequestDTO.getUserPassword();
    //    String checkPassword = userRegisterRequestDTO.getCheckPassword();
    //
    //    if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
    //        return BaseResponse.error("账号/密码/校验密码为空");
    //    }
    //    long id = userService.userRegister(userAccount, userPassword, checkPassword);
    //
    //
    //    return BaseResponse.success(id);
    //}


    ///**
    // *
    // * 用户登录
    // *
    // * @param AdminUserDTO 用户传参
    // * @param request http
    // * @return 返回脱敏用户信息
    // */
    //@ApiOperation("用户登录")
    //@PostMapping("/login")
    //public BaseResponse<AdminUser> userLogin(@RequestBody AdminUserDTO adminUserDTO, HttpServletRequest request) {
    //
    //    //校验参数
    //    if (adminUserDTO == null) {
    //        return BaseResponse.errorResult( ErrorCode.ARGUMENT_ERROR);
    //    }
    //
    //    //获取用户名密码
    //    String userAccount = adminUserDTO.getAccount();
    //    String userPassword = adminUserDTO.getPassword();
    //
    //
    //    if (StringUtils.isAnyBlank(userAccount, userPassword)) {
    //        return BaseResponse.errorResult( -1, "用户账号或密码为空");
    //    }
    //
    //    //登录
    //    AdminUser user = adminUserService.adminUserLogin(userAccount, userPassword, request);
    //
    //    return BaseResponse.okResult(user);
    //
    //}


    /**
     *
     * 用户登录
     *
     * @param agentUser 用户传参
     * @param request http
     * @return 返回脱敏用户信息
     */
    @ApiOperation("商家添加")
    @PostMapping("/add")
    public BaseResponse addAgetnUser(@RequestBody AgentUserAddDTO agentUser, HttpServletRequest request) {

        //校验参数
        if (agentUser == null) {
            return BaseResponse.errorResult( ErrorCode.ARGUMENT_ERROR);
        }

        //获取用户名密码
        if (StringUtils.isAnyBlank(agentUser.getAccount(), agentUser.getPassword())) {
            return BaseResponse.errorResult( -1, "必须输入账号和密码");
        }

        //添加
        boolean res = agentUserService.addAgetnUser(agentUser, request);

        return BaseResponse.okResult(res);

    }



    /**
     *
     * 修改用户信息
     *
     * @param agentUser 用户传参
     * @param request http
     * @return 返回是否修改成功
     */
    @ApiOperation("修改商家信息")
    @PostMapping("/update")
    public BaseResponse updateAgetnUser(@RequestBody AgentUser agentUser, HttpServletRequest request) {

        //校验参数
        if (agentUser == null || agentUser.getId() == null) {
            return BaseResponse.errorResult( ErrorCode.ARGUMENT_ERROR);
        }

        //修改
        boolean res = agentUserService.updateAgetnUser(agentUser, request);

        return BaseResponse.okResult(res);

    }


    /**
     *
     * 根据用户名查询
     *
     * @param agentUserDTO
     * @return
     */
    @ApiOperation("查询商家信息")
    @PostMapping("/search")
    public BaseResponse<Page<AgentUser>> searchUser(@RequestBody AgentUserSearchDTO agentUserDTO, HttpServletRequest request) {
        //
        //@ApiModelProperty(value = "昵称")
        //private String nickname;
        //
        //@ApiModelProperty(value = "添加时间")
        //private Long createTime;

        //@ApiModelProperty(value = "0:有效 1:禁止登录")
        //private Boolean isDisable;
        //
        //@ApiModelProperty(value = "审核状态(1-待审核;2-已审核;3-拒绝)")
        //private Integer inspectStatus;
        //联系电话
        //@ApiModelProperty(value = "联系方式")
        //private String phone;
        //
        ////账号类型
        //@ApiModelProperty(value = "账号类型")
        //private String accountType;
        //
        //@ApiModelProperty(value = "运营类型")
        //private String operateType;
        //
        //@ApiModelProperty(value = "共享类型")
        //private String shareType;

        System.out.println();
        Page<AgentUser> agentUserList = agentUserService.searchUser(agentUserDTO);

        return BaseResponse.okResult(agentUserList);
    }


    @PostMapping("/delete")
    @ApiOperation("删除商家信息")
    public BaseResponse deleUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        log.info("进入信息删除, id{}", deleteRequest.getId());

        if (deleteRequest.getId() <= 0) {
            return BaseResponse.errorResult(ErrorCode.PARAM_ERR);
        }


        boolean remove = agentUserService.lambdaUpdate().
                eq(AgentUser::getId, deleteRequest.getId()).
                remove();

        return BaseResponse.okResult(remove);

    }
    //
    //
    ///**
    // *
    // * 获取用户登录状态
    // *
    // * @param request
    // * @return
    // */
    //@GetMapping("/current")
    //public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
    //    User user = (User)request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
    //
    //    if (user == null) {
    //        return BaseResponse.error("session中用户状态为空");
    //    }
    //
    //    // todo 这里后来要确认用户状态，如果用户是被封号的，那么是不能登录，没有状态的
    //    //脱敏
    //    return BaseResponse.success(userService.getSafteUser(userService.lambdaQuery().eq(User::getId, user.getId()).one()));
    //}
    //
    //
    ///**
    // *
    // * 退出登录
    // *
    // * @param request
    // * @return
    // */
    //@PostMapping("/outLogin")
    //public BaseResponse<Boolean> outLogin(HttpServletRequest request) {
    //    request.getSession().removeAttribute(UserConstant.USER_LOGIN_STATE);
    //    return BaseResponse.success(true);
    //}
    //
    //
    ///**
    // *
    // * 鉴定权限
    // *
    // * @param request
    // * @return
    // */
    //private boolean isAdmin(HttpServletRequest request) {
    //    User user = (User)request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
    //
    //    // todo 后期需要处理，这里需要抛出异常
    //    if (user == null || user.getUserRole() != UserConstant.ADMIN_ROLE) {
    //        log.info("权限不够，仅仅管理员可操作");
    //        return false;
    //    }
    //
    //    return true;
    //}


}
